﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModelORM;
using System.Data.Entity;

namespace Zarzadzacz
{
    class ProduktZarzadzator : Zarzadzator<tbl_produkt>
    {
        //IData
        public override List<tbl_produkt> GetData()
        {
            using (MagazynEntities context = new MagazynEntities())
            {
                List<tbl_produkt> lista;
                try
                {
                    //zwraca tylko dostępne produkty
                    lista = (from p in context.tbl_produkt
                             where p.Dostepnosc == true
                             select p).ToList();

                }
                catch (Exception ex)
                {
                    lista = new List<tbl_produkt>();
                }

                return lista;
            }
        }

        public override tbl_produkt GetData(int dataId)
        {
            using (MagazynEntities context = new MagazynEntities())
            {
                tbl_produkt entity;
                try
                {
                    entity = (from k in context.tbl_produkt
                              where k.ID == dataId
                              select k).FirstOrDefault();


                }
                catch (Exception ex)
                {
                    entity = null;
                }

                return entity;
            }
        }

        //IUpdateData
        public override bool UpdateData(tbl_produkt dataToUpdate)
        {
            using (MagazynEntities context = new MagazynEntities())
            {
                try
                {
                    var entity = (from p in context.tbl_produkt
                                  where p.ID == dataToUpdate.ID
                                  select p).FirstOrDefault();

                    entity.IDDostawcy = dataToUpdate.IDDostawcy;
                    entity.IDKategorii = dataToUpdate.IDKategorii;
                    entity.IDSektora = dataToUpdate.IDSektora;
                    entity.Nazwa = dataToUpdate.Nazwa;
                    entity.Opis = dataToUpdate.Opis;
                    entity.Cena = dataToUpdate.Cena;
                    entity.Dostepnosc = dataToUpdate.Dostepnosc;

                    

                    context.SaveChanges();

                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public override bool DeleteData(int dataId)
        {
            using (MagazynEntities context = new MagazynEntities())
            {
                try
                {
                    var entity = (from k in context.tbl_produkt
                                  where k.ID == dataId
                                  select k).FirstOrDefault();
                    context.tbl_produkt.DeleteObject(entity);
                    context.SaveChanges();

                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
